<template>
  <div class="login-wrap" v-if="bindFlag === 'false'">
    <div class="ms-login">
      <div class="ms-title">绑定手机</div>
      <el-form
          :model="param"
          :rules="rules"
          ref="login"
          label-width="0px"
          class="ms-content"
      >
        <el-form-item prop="mobile">
          <el-input v-model="param.mobile" placeholder="请输入手机号">
            <template #prepend>
              <el-button icon="el-icon-phone"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-input
              placeholder="请输入验证码"
              v-model="param.captcha"
              @keyup.enter="smsSubmitForm()"
          >
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">
            <span v-show="show" @click="getCode">获取验证码</span>
            <span v-show="!show" class="count"
            >{{ count }} s</span
            ></el-button
          >
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">绑定</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {thirdBindGrantEditUserApi} from "../api/mgt/system/thirdBindGrant";
import {getAccessTokenApi} from "../api/mgt/power/auth";
import {smsSendCaptchaApi} from "../api/setup/sms";

const clientId = process.env.VUE_APP_CLIENT_ID;
const clientSecret = process.env.VUE_APP_CLIENT_SECRET;
const smsTplCode = process.env.VUE_APP_SMS_TPL_CODE;
const mgtOssTplCode = process.env.VUE_APP_MGT_OSS_TPL_CODE;
export default {
  data() {
    return {
      show: true,
      count: "",
      timer: null,
      bindFlag: false,
      loginParam: {
        code: "",
        client_id: clientId,
        client_secret: clientSecret,
        grant_type: "bind_code",
      },
      param: {
        code: "",
        smsTplCode: smsTplCode,
        ossTplCode: mgtOssTplCode,
        mobile: "",
        captcha: "",
        nickName: "",
        avatar: "",
      },
      rules: {
        mobile: [
          {required: true, message: "请输入手机号", trigger: "change"},
          {max: 11, message: "长度不能超过11个字符", trigger: "change"},
        ],
        captcha: [
          {required: true, message: "请输入验证码", trigger: "change"},
          {max: 6, message: "长度不能超过6个字符", trigger: "change"},
        ],
      },
    };
  },
  created() {
    const {code, bindFlag, nickName, avatar} = this.$route.query;
    this.loginParam.code = code;
    this.param.code = code;
    this.param.nickName = nickName;
    this.param.avatar = avatar;
    this.bindFlag = bindFlag;
    if (this.bindFlag === "true") {
      this.loginForm();
    }
  },
  methods: {
    loginForm() {
      getAccessTokenApi(this.loginParam).then((res) => {
        const data = {...res};
        this.$store.commit("setUserToken", data);
        this.$router.push("/");
      });
    },
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          thirdBindGrantEditUserApi(this.param).then((res) => {
            this.loginForm();
          });
        } else {
          return false;
        }
      });
    },
    getCode() {
      smsSendCaptchaApi({
        tplCode: smsTplCode,
        mobile: this.param.mobile,
      });
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../assets/img/login-bg.jpg);
  background-size: 100%;
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #000;
  border-bottom: 1px solid #ddd;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(231, 210, 227, 1);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
